package com.aos.erp.manager.rest.applyschool;


import com.aos.erp.common.GlobalConstant;
import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.common.redis.RedisService;
import com.aos.erp.manager.enums.SmsType;
import com.aos.erp.manager.model.applyschool.ro.*;
import com.aos.erp.manager.model.applyschool.vo.ApplyInfoVo;
import com.aos.erp.manager.model.applyschool.vo.ApplyListVo;
import com.aos.erp.manager.model.applyschool.vo.SchoolNameVo;
import com.aos.erp.manager.remote.MessageService;
import com.aos.erp.manager.service.applyschool.ApplySchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.text.ParseException;

/**
 * <p>
 * 驾校注册申请记录 前端控制器
 * </p>
 *
 * @author 杨文博
 * @since 2018-12-02
 */
@Slf4j
@RestController
@RequestMapping("/api/apply")
@Api(value="驾校注册申请记录",tags={"驾校注册申请记录"})
public class ApplySchoolController {

	@Autowired
	private ApplySchoolService applySchoolService;


	@GetMapping("/list")
	@ApiOperation(value = "驾校注册申请记录分页查询")
	public ResultEntity<IPageBean<ApplyListVo>> queryPageBean(QueryApplySchoolRo queryGatherSchoolRo) {
		return ResultEntity.success(applySchoolService.queryByPage(queryGatherSchoolRo));
	}


/**
	 * 查看--获取驾校信息
	 * @[param id  驾校id
	 * @return
	 */
	@ApiOperation(value = "获取驾校信息", notes = "获取驾校信息")
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@ApiImplicitParam(name = "id",value = "驾校id",required = true,dataType ="Long",paramType ="query" )
	public ResultEntity<ApplyInfoVo> gatherInfo(@NotNull(message = "参数不能为空") long id) throws BizException {
		return ResultEntity.success(applySchoolService.applyInfo(id));
	}


	/**
	 * 审核驾校
	 * @[param reviewApply  审核实体
	 * @return
	 */
	@ApiOperation(value = "审核驾校", notes = "审核驾校")
	@RequestMapping(value = "/reviewApply",method = RequestMethod.POST)
	public ResultEntity  reviewApply(@Validated ReviewApplyRo reviewApply, BindingResult bindingResult) throws Exception {
		//数据 验证信息
		if (bindingResult.hasErrors()) {
			return  ResultEntity.failed(bindingResult.getFieldErrors().get(0).getDefaultMessage());
		}
		applySchoolService.editApply(reviewApply);
		return  ResultEntity.success();
	}


	/**
	 * 根据总校代码获取总校名称
	 * @[param code 总校Code
	 * @return
	 */
	@ApiOperation(value = "根据总校代码获取总校名称", notes = "根据总校代码获取总校名称")
	@RequestMapping(value = "/gatherNameByCode", method = RequestMethod.POST)
	@ApiImplicitParam(name = "code",value = "总校Code",required = true,dataType ="String",paramType ="query" )
	public ResultEntity<SchoolNameVo> gatherNameByCode(@NotNull(message = "参数不能为空") String code) throws BizException {
		return ResultEntity.success(applySchoolService.gatherNameByCode(code));
	}


	/**
	 * 根据总校代码获取总校名称(忽略)
	 * @[param code 总校Code
	 * @return
	 */
	@ApiOperation(value = "根据总校代码获取总校名称（忽略）", notes = "根据总校代码获取总校名称（忽略）")
	@RequestMapping(value = GlobalConstant.GATEWAY_IGNORE+"/gatherNameByCode", method = RequestMethod.POST)
	@ApiImplicitParam(name = "code",value = "总校Code",required = true,paramType ="query" )
	public ResultEntity<SchoolNameVo> gatherNameByCodeHl(@NotNull(message = "参数不能为空") String code) throws BizException {
		return ResultEntity.success(applySchoolService.gatherNameByCode(code));
	}


	/**
	 * 查询审核结果
	 * @[param code 总校Code
	 * @return
	 */
	@ApiOperation(value = "查询审核结果", notes = "查询审核结果")
	@RequestMapping(value =  GlobalConstant.GATEWAY_IGNORE+"/queryResult", method = RequestMethod.POST)
	public ResultEntity queryResult(LoginUserRo loginUserRo, BindingResult bindingResult) throws BizException {
		//数据 验证信息
		if (bindingResult.hasErrors()) {
			return  ResultEntity.failed(bindingResult.getFieldErrors().get(0).getDefaultMessage());
		}
		return ResultEntity.success(applySchoolService.queryResult(loginUserRo));
	}


	/**
	 * 申请驾校
	 * @[param code 总校Code
	 * @return
	 */
	@ApiOperation(value = "申请驾校", notes = "申请驾校")
	@RequestMapping(value = GlobalConstant.GATEWAY_IGNORE+ "/applySchool", method = RequestMethod.POST)
	public ResultEntity applySchool(@Valid ApplySchoolRo applySchoolRo, BindingResult bindingResult) throws BizException {
		//数据 验证信息
		if (bindingResult.hasErrors()) {
			return  ResultEntity.failed(bindingResult.getFieldErrors().get(0).getDefaultMessage());
		}
		applySchoolService.applySchool(applySchoolRo);
		return ResultEntity.success();
	}



	/**
	 * 修改申请-驾校
	 * @[param code 总校Code
	 * @return
	 */
	@ApiOperation(value = "修改申请-驾校", notes = "修改申请-驾校")
	@RequestMapping(value =  GlobalConstant.GATEWAY_IGNORE+"/updateApplySchool", method = RequestMethod.POST)
	public ResultEntity updateApplySchool(@Valid UpdateApplySchoolRo updateApplySchoolRo, BindingResult bindingResult) throws BizException {
		//数据 验证信息
		if (bindingResult.hasErrors()) {
			return  ResultEntity.failed(bindingResult.getFieldErrors().get(0).getDefaultMessage());
		}
		applySchoolService.updateApplySchool(updateApplySchoolRo);
		return ResultEntity.success();
	}


	/**
	 * 发送短信验证码-注册驾校
	 * @[param code 总校Code
	 * @return
	 */
	@ApiOperation(value = "发送验证码-注册驾校", notes = "发送验证码-注册驾校")
	@RequestMapping(value = GlobalConstant.GATEWAY_IGNORE+ "/sendSmsMessage", method = RequestMethod.POST)
	public ResultEntity sendSmsMessage(String phone){
		applySchoolService.sendSmsMessage(phone);
		return ResultEntity.success();
	}

	/**
	 * 验证短信验证码-注册驾校
	 * @param code 验证码
	 * @param phone 电话号码
	 * @return
	 */
	@ApiOperation(value = "验证验证码-注册驾校", notes = "验证验证码-注册驾校")
	@RequestMapping(value = GlobalConstant.GATEWAY_IGNORE+ "/checkSmsMessage", method = RequestMethod.POST)
	public ResultEntity checkSmsMessage(String phone,String code) throws BizException, ParseException {
		applySchoolService.checkSmsMessage(phone,code);
		return ResultEntity.success();
	}




}

